<?php

namespace backend\models\system;

use Yii;

/**
 * This is the model class for table "edu_friend_link".
 *
 * @property integer $link_id
 * @property string $link_name
 * @property string $link_url
 * @property string $link_logo
 * @property integer $link_order
 */
class FriendLink extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%friend_link}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['link_name', 'link_url', 'link_logo'], 'required'],
            [['link_order'], 'integer'],
            [['link_name'], 'string', 'max' => 30],
            [['link_url'], 'string', 'max' => 50],
            [['link_logo'], 'file','extensions'=>['jpg','png','gif']]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
			'link_id' => '',
			'link_name' => '名称',
			'link_url' => '链接',
			'link_logo' => 'Logo',
			'link_order' => '排序',
        ];
    }

    /**
     * @inheritdoc   友情链接查询
     */
    public function friendlinksel()
    {
        return $this->find()->asArray()->orderBy("link_order desc")->all();
    }

	/**
	 * @inheritdoc   友情链接查询  limit
	 */
	public function friendlinksellimit()
	{
		return $this->find()->limit(6)->orderBy("link_order desc")->asArray()->all();
	}

	/**
	 * @inheritdoc   友情链接单条查询
	 */
	public function friendlinkByid()
	{
		return $this->find()
			->select(['link_id','link_name','link_url','link_logo','link_order'])
			->where(['link_id'=>$this->link_id])
			->asArray()->one();
	}

	/**
	 * @inheritdoc   友情链接修改
	 */
	static function friendlinksave()
	{
		$link = Yii::$app->request->post('FriendLink');
		$model = self::find()->where(['link_id'=>$link['link_id']])->one();
		$model->attributes = $link;
		return $model->save();
	}
}
